package com.htx.oldbookpavilion.web.mycenter;


import com.htx.oldbookpavilion.common.StandardPageData;
import com.htx.oldbookpavilion.common.StandardResponse;
import com.htx.oldbookpavilion.entity.ShoppingList;
import com.htx.oldbookpavilion.service.mycenter.ShoppingListService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/manager")
public class ShoppingListController {

    Logger logger = LoggerFactory.getLogger(ShoppingListController.class);

    @Autowired
    private ShoppingListService shoppingListService;

    /**
     * 分页查询购书清单
     * @param sPageData
     * @return
     */
    @RequestMapping("/getShoppingListByPage")
    public StandardResponse getShoppingListByPage(@RequestBody StandardPageData<ShoppingList> sPageData){
        StandardResponse<StandardPageData<ShoppingList>> sResponse = new StandardResponse<>();
        sPageData.setStartIndex(sPageData.calStartIndex());
        shoppingListService.getShoppingListByPage(sPageData);
        if (sPageData.getRecords() != null) {
            sResponse.setSuccess(true);
            sResponse.setMsg("分页获取购书清单成功");
        }else{
            sResponse.setSuccess(false);
            sResponse.setMsg("获取购书清单失败");
        }
        sResponse.setData(sPageData);
        return sResponse;
    }

    /**
     * 删除购书清单数据
     * @param shoppingLists
     * @return
     */
    @RequestMapping("/deleteShoppingList")
    public StandardResponse deleteShoppingList(@RequestBody List<ShoppingList> shoppingLists) {
        StandardResponse<Boolean> sResponse = new StandardResponse<>();
        if (shoppingLists == null || shoppingLists.size() == 0) {
            logger.error("删除购书清单条目时，传递参数为空");
            sResponse.setSuccess(false);
            sResponse.setMsg("传递参数不能为空");
            sResponse.setData(false);
        }
        boolean isDeleted = shoppingListService.deleteShoppingList(shoppingLists);
        if (isDeleted) {
            sResponse.setSuccess(true);
            sResponse.setMsg("删除成功");
            sResponse.setData(true);
        }else{
            sResponse.setSuccess(false);
            sResponse.setMsg("删除失败");
            sResponse.setData(false);
        }
        return sResponse;
    }

    /**
     * 更新购书清单数据
     * @param shoppingLists
     * @return
     */
    @RequestMapping("/updateShoppingList")
    public StandardResponse updateShoppingList(@RequestBody List<ShoppingList> shoppingLists) {
        StandardResponse<Boolean> sResponse = new StandardResponse<>();
        if (shoppingLists == null || shoppingLists.size() == 0) {
            logger.error("更新购书清单条目时，传递参数为空");
            sResponse.setSuccess(false);
            sResponse.setMsg("传递参数不能为空");
            sResponse.setData(false);
        }
        boolean isUpdated = shoppingListService.updateShoppingList(shoppingLists);
        if (isUpdated) {
            sResponse.setSuccess(true);
            sResponse.setMsg("更新成功");
            sResponse.setData(true);
        }else{
            sResponse.setSuccess(false);
            sResponse.setMsg("更新失败");
            sResponse.setData(false);
        }
        return sResponse;
    }


}
